import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import ChatDriver
from selenium.webdriver.support import expected_conditions as EC


class ForumLogin:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:9580/sign-in.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

    def ForumLoginByTest(self):
        # 分别查找用户名输入框、密码输入框、登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

    # 正常登录状态测试用例
    def ForumLoginSucTest(self, password):
        # 先清除输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 找到用户输入框并输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsans")
        # 找到密码输入框并输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 登录成功会跳转至系统内，只需要确认是否有用户昵称
        element = WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col")))
        # self.driver.find_element(By.CSS_SELECTOR, "#index_nav_nickname")
        time.sleep(1)
        # 添加屏幕截图
        ChatDriver.get_alert_image()

    # 异常状态下的测试用例
    def ForumLoginFailTest(self):
        # 正确账号,错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsans")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456789")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))  # 弹窗警告
        time.sleep(1)
        ChatDriver.get_alert_image()

        # 错误账号,正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsansssssss")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))  # 弹窗警告
        time.sleep(1)
        ChatDriver.get_alert_image()

        # 错误账号,错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsansssssss")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))  # 弹窗警告
        time.sleep(1)
        ChatDriver.get_alert_image()

        # 填写账号,密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsans")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-2 > div > div")  # 出现密码不能为空元素
        time.sleep(1)
        ChatDriver.get_alert_image()

        # 账号为空,密码填写
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-3 > div")  # 出现账号不能为空元素
        time.sleep(1)
        ChatDriver.get_alert_image()  # #signInForm > div.mb-3 > div

        # 账号为空,密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-3 > div")  # 出现账号不能为空元素
        self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-2 > div > div")  # 出现密码不能为空元素
        time.sleep(1)
        ChatDriver.get_alert_image()  # #signInForm > div.mb-3 > div
